/*
 * RenderSystem.cpp
 *
 *  Created on: 2009/01/05
 *      Author: Yongshik Cho
 */

#include "RenderSystem.h"
#include "xml/AltoSettings.h"
#include "device/GLDevice.h"
#include "event/MouseEvent.h"
#include "event/KeyEvent.h"
#include "event/TickEvent.h"
#include "Exception.h"


RenderSystem::RenderSystem(const HWND hWnd, const Rect2D& rect, DeviceSettings* sets)
{
	if(sets->renderdevice == AltoSettings::_OPENGL_TAG)
	{
		mDevice = new GLDevice(hWnd, rect, sets);
		if(!mDevice)
			AltoThrow(Exception::ERR_MEMORY);
	}

	mSystemTimer = new SystemTimer();
	if(!mSystemTimer)
		AltoThrow(Exception::ERR_MEMORY);

	mSystemFontsManager = new SystemFontsManager();
	if(!mSystemFontsManager)
		AltoThrow(Exception::ERR_MEMORY);

	mTickableManager = new TickableManager();
	if(!mTickableManager)
		AltoThrow(Exception::ERR_MEMORY);

	mParticleManager = new ParticleManager();
	if(!mParticleManager)
		AltoThrow(Exception::ERR_MEMORY);

	mWorldManager = new WorldManager();
	if(!mWorldManager)
		AltoThrow(Exception::ERR_MEMORY);

	mSceneManager = new SceneManager();
	if(!mSceneManager)
		AltoThrow(Exception::ERR_MEMORY);
}

RenderSystem::~RenderSystem()
{
	// TODO Auto-generated destructor stub
	SAFE_DELETE(mSceneManager);
	SAFE_DELETE(mWorldManager);
	SAFE_DELETE(mParticleManager);
	SAFE_DELETE(mTickableManager);
	SAFE_DELETE(mSystemFontsManager);
	SAFE_DELETE(mSystemTimer);
	SAFE_DELETE(mDevice);
}

void RenderSystem::render()
{
	if(!mDevice || !mSceneManager)
		return;

	mSceneManager->clear();
	mSceneManager->render();
	mDevice->swapBuffers();
}

void RenderSystem::tick()
{
	if(mTickableManager)
		mTickableManager->tick();

	if(mParticleManager)
		mParticleManager->tick();

	if(mWorldManager)
		mWorldManager->tick();
}
